const { findUserOne, findAdmin } = require('../model/User')
const { sign } = require('../utils/jwt')
module.exports = {
  /* 获取用户信息 */
  async getUserInfo (req, res, next) {
    try {
      const result = await findUserOne(req.query.id)
      if (result.length) {
        const [user] = result
        return res.success('请求成功', 200, user)
      }
      return res.error('用户不存在')
    } catch (err) {
      return next(err)
    }
  },

  /* 管理员登录 */
  async amdinLogin (req, res, next) {
    try {
      const result = await findAdmin(req.body)
      if (result.length) {
        const [{ user }] = result
        const signdata = { user }
        const token = sign(signdata)
        return res.success('登录成功!', 200, token)
      }
      return res.error('账户/密码错误')
    } catch (err) {
      return next(err)
    }
  }
}